package com.sg.fileService.util;

import java.io.IOException;
import java.io.OutputStream;

import com.sg.fileService.model.EnumSize;

import com.sg.fileService.config.Property;

import net.coobird.thumbnailator.Thumbnails;

public class ImageEncoder {
	private ImageEncoder() {
		
	}
	/**
	 * 最终所有图片都需要转换成的类型
	 */
	private static final String IMG_SUFFIX = Property.IMG_FORMAT_TYPE;

	/**
	 * 转码
	 * 
	 * @param source
	 *            源文件路径
	 * @throws IOException
	 */
	public static void toTranscoding(String source) throws IOException {

		Thumbnails.of(source).scale(1f).outputFormat(IMG_SUFFIX).toFile(
				PathCreator.getTargetFilePath(PathCreator.modifyFileSuffix(source, IMG_SUFFIX), EnumSize.BIG_SIZE));

	}

	/**
	 * 转码并输出
	 * 
	 * @param source
	 *            源文件路径
	 * @param suffix
	 *            后缀
	 * @param os
	 *            文件流
	 * @throws IOException
	 */
	public static void toTranscodingAndOutput(String source, String suffix, OutputStream os) throws IOException {


		Thumbnails.of(source).scale(1f).outputFormat(suffix).toOutputStream(os);


	}

	/**
	 * 转码缩放并输出
	 * 
	 * @param source
	 *            源文件路径
	 * @param suffix
	 *            后缀
	 * @param width
	 *            文件宽
	 * @param height
	 *            文件高
	 * @param os
	 *            文件流
	 * @throws IOException
	 */
	public static void toTranscodingAndZoomAndOutput(String source, String suffix, int width, int height,
			OutputStream os) throws IOException {
		Thumbnails.of(source).forceSize(width, height).outputFormat(suffix).toOutputStream(os);
	}

	/**
	 * 将源文件生成缩略图
	 * 
	 * @param source
	 * @throws IOException
	 */
	public static void toBreviary(String source) throws IOException {
		Thumbnails.of(source).size(Property.IMG_BREVIARY_WIDTH, Property.IMG_BREVIARY_HEIGHT)
				.toFile(PathCreator.getTargetFilePath(source, EnumSize.BREVIARY_IMG));
	}

	/**
	 * 将源文件生成IMG_SUFFIX后缀的缩略图
	 * 
	 * @param source
	 * @throws IOException
	 */
	public static void toBreviaryFormat(String source) throws IOException {


		Thumbnails.of(source).size(Property.IMG_BREVIARY_WIDTH, Property.IMG_BREVIARY_HEIGHT).outputFormat(IMG_SUFFIX)
				.toFile(PathCreator.getTargetFilePath(PathCreator.modifyFileSuffix(source, IMG_SUFFIX),
						EnumSize.BREVIARY_IMG));

	}

	/**
	 * 将源文件生成小图
	 * 
	 * @param source
	 * @throws IOException
	 */
	public static void toSmall(String source) throws IOException {

		Thumbnails.of(source).size(Property.IMG_SMALL_WIDTH, Property.IMG_SMALL_HEIGHT)
				.toFile(PathCreator.getTargetFilePath(source, EnumSize.MINI_SIZE));
	}

	/**
	 * 将源文件生成IMG_SUFFIX后缀的小图
	 * 
	 * @param source
	 * @throws IOException
	 */
	public static void toSmallFormat(String source) throws IOException {

		Thumbnails.of(source).size(Property.IMG_SMALL_WIDTH, Property.IMG_SMALL_HEIGHT).outputFormat(IMG_SUFFIX).toFile(
				PathCreator.getTargetFilePath(PathCreator.modifyFileSuffix(source, IMG_SUFFIX), EnumSize.MINI_SIZE));

	}

	/**
	 * 将源文件生成IMG_SUFFIX后缀的中等图
	 * 
	 * @param source
	 * @throws IOException
	 */
	public static void toMiddle(String source) throws IOException {

		Thumbnails.of(source).size(Property.IMG_MIDDLE_WIDTH, Property.IMG_MIDDLE_HEIGHT)
				.toFile(PathCreator.getTargetFilePath(source, EnumSize.MIDDLE_SIZE));

	}

	/**
	 * 将源文件生成IMG_SUFFIX后缀的中等图
	 * 
	 * @param source
	 * @throws IOException
	 */
	public static void toMiddleFormat(String source) throws IOException {

		Thumbnails.of(source).size(Property.IMG_MIDDLE_WIDTH, Property.IMG_MIDDLE_HEIGHT).outputFormat(IMG_SUFFIX)
				.toFile(PathCreator.getTargetFilePath(PathCreator.modifyFileSuffix(source, IMG_SUFFIX),
						EnumSize.MIDDLE_SIZE));
	}
}
